# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  
  # no logging in, no need for sessions for now
  session :disabled => true
  # since no session and no forms, taking this out:
  # protect_from_forgery # :secret => '08085826cbbfe07f12d45bc6a1c38126'
  
  
  
  protected
  
  # Open Flash Chart pie graph with our default colors and fonts
  def ofc_pie_graph_with_defaults(collection, attribute, member_title, graph_title, links = [])
		ofc = Graph.new
  	ofc.title graph_title, '{font-size:20px; color: #EC7334; padding:5px;}'
  	counts = collection.map(&attribute.to_sym)
  	sum = counts.sum.to_f
  	data = counts.map { |c| c / sum * 100 }.map { |n| n.round(2) }
  	ofc.pie 60, '#996666', '{color:#339933;font-size:11px;}'
    #links = 
  	ofc.pie_values data,  collection.map(&member_title.to_sym), links
  	ofc.pie_slice_colors %w(#dd9966 #66FF66 #0066FF #9900FF #FF0099 #FF0000 #009900 #000066)
  	ofc.set_bg_color '#ffffff'
  	ofc.set_tool_tip '#x_label#<br>#val#%'
  	ofc
	end
	
  # Open Flash Chart bar graph with our default colors and fonts
	def ofc_bar_graph_with_defaults(collection, attribute, member_title, graph_title, xlabel, 
        y_legend = "Time (in milliseconds)")
		ofc = Graph.new
  	ofc.title graph_title, '{font-size:20px; color: #EC7334; padding:5px;}'
  	ofc.set_data collection.map(&attribute.to_sym)
	  ofc.bar_glass 55, '#f3ae2a', '#669966', xlabel, 10
	  max_value = collection.map(&attribute.to_sym).max
	  # sets the y max to be the next highest number divisble by 50 at lower maxes, and divisible by 100 for the rest
	  set_max = max_value <= 100 ? (((max_value / 50.0) + 1).to_i * 50) : (((max_value / 100.0) + 1).to_i * 100)
	  ofc.set_y_max set_max
		ofc.set_y_label_steps 10
		ofc.set_y_legend y_legend, 12, "#996666"
		ofc.set_x_labels collection.map(&member_title.to_sym).map { |name| name.gsub(" ", "\n") }
		ofc.set_x_label_style 11, "#996666", 2
		ofc.set_bg_color '#ffffff'
		ofc.set_tool_tip '#val#ms'
		ofc
	end

  
  # Open Flash Chart candle graph with our default colors and fonts
  # (not really ideal for this app, was trying to adopt it as a box chart)
  def ofc_candle_graph_with_defaults(collection, high_attr, average_attr, low_attr, member_title, graph_title, xlegend, 
        y_legend = "Time (in milliseconds)")
    ofc = Graph.new
    ofc.title graph_title, '{font-size:20px; color: #EC7334; padding:5px;}'
    candles = []
    for obj in collection
      candles << Candle.new( obj.send(high_attr.to_sym), obj.send(average_attr.to_sym)+100, obj.send(average_attr.to_sym),
                                obj.send(low_attr.to_sym)  ) 
    end
    ofc.candle(candles, 60, 2, '#c11b01', graph_title, 12)
    max_value = collection.map(&high_attr.to_sym).max
    set_max = max_value <= 100 ? (((max_value / 50.0) + 1).to_i * 50) : (((max_value / 100.0) + 1).to_i * 100)
    ofc.set_y_max set_max
		ofc.set_y_label_steps 10
		ofc.set_y_legend y_legend, 12, "#996666"
		ofc.set_x_labels collection.map(&member_title.to_sym).map { |name| name.gsub(" ", "\n") }
    ofc.set_tool_tip('#x_legend#<br>High: #high#<br>Open: #open#<br>Close: #close#<br>Low: #low#' )
		ofc.set_x_label_style 11, "#996666", 2
    ofc.set_x_legend(xlegend)
		ofc.set_bg_color '#ffffff'
    ofc
  end
end
